package com.lungle.warmdiary.app.view.testcampaign.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.BaseViewPagerAdapter;
import com.lungle.warmdiary.app.model.event.CampaignGoToDetail;
import com.lungle.warmdiary.app.model.event.CampaignGoToDetailListRate;
import com.lungle.warmdiary.app.ui.tab.SlidingTabLayout;
import com.lungle.warmdiary.app.view.IPagerFragment;
import com.lungle.warmdiary.app.view.testcampaign.adapter.CampaignDetailPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by zhangxueyuan on 2016/9/28.
 * <p>
 * 亲测详情
 */
public class CampaignDetailPagerFragment extends Fragment implements
        IPagerFragment {


    private ViewPager mViewPager;
    private CampaignDetailPagerAdapter mTabAdapter;
    private SlidingTabLayout mSlidingTabLayout;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_viewpager4, container,
                false);

        mSlidingTabLayout = (SlidingTabLayout) getActivity().findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.tab_indicator_detail, R.id.tv_media_name);
        mViewPager = (ViewPager) view.findViewById(R.id.active_detail_tab_pager);
        mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.view_pager_margin));

        final Resources res = getResources();
        mSlidingTabLayout.setSelectedIndicatorColors(res.getColor(R.color.white));
        mSlidingTabLayout.setDistributeEvenly(true);

        if (mTabAdapter == null) {
            mTabAdapter = new CampaignDetailPagerAdapter(getChildFragmentManager());
        }
        mViewPager.setOffscreenPageLimit(mTabAdapter.getCount());
        mViewPager.setAdapter(mTabAdapter);

        // TODO: 16/10/5 想让主页面不滑动。 
//        mViewPager.canScrollHorizontally(0);

        mSlidingTabLayout.setViewPager(mViewPager);


        return view;
    }

    @Override
    public BaseViewPagerAdapter getPagerAdapter() {
        return mTabAdapter;
    }

    @Override
    public ViewPager getViewPager() {
        return mViewPager;
    }

    @Override
    public Fragment getCurrentFragment() {
        return mTabAdapter.getItemAt(mViewPager.getCurrentItem());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void gotoDetailList(CampaignGoToDetail event) {
        mViewPager.setCurrentItem(1);
        EventBus.getDefault().post(new CampaignGoToDetailListRate(0));
    }


}